package org.jsemantic.support.examples.even;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;

import org.jservicerules.support.semanticservice.StatelessSemanticService;
import org.jservicerules.support.spring.SpringContextFactory;
import org.springframework.context.ApplicationContext;

public class EvenStressTest {

	private static final org.apache.log4j.Logger log = org.apache.log4j.Logger
			.getLogger(EvenStressTest.class);

	private static ApplicationContext ctx = SpringContextFactory
			.getClassPathApplicationContexFactory("org/jsemantic/support/examples/even/services.xml");

	private static StatelessSemanticService service = null;

	public static class ThreadHolder implements Runnable {

		public void run() {

			Collection results = service.execute(getFacts());

			if (log.isDebugEnabled()) {
				log.debug(results);
			}
			if (log.isInfoEnabled()) {
				log.info(results);
			}
			results.clear();
			results = null;

		}
	};

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		service = (StatelessSemanticService) ctx.getBean("evenService");

		while (true) {
			new Thread(new ThreadHolder()).start();
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		}

	}

	private static Collection getFacts() {
		Random r = new Random();
		List facts = new ArrayList();
		for (int i = 0; i < 10; i++) {
			facts.add(new Integer(r.nextInt() % 100));
		}
		return facts;
	}

}
